/**
 * Copyright 2020 Inc.
 **/
package com.myz.cloud.rocketmq.consumer.mq;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.myz.cloud.rocketmq.config.Order;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.springframework.stereotype.Component;

/**
 * 消费顺序消息 one queue, one thread
 * consumeMode：默认并发执行，设置ORDERLY
 * 需要开启consumeMode=ConsumeMode.ORDERLY
 *
 * @author maoyz0621 on 2020/8/5
 * @version v1.0
 */
@RocketMQMessageListener(topic = "${rocketmq.topic.orderly}",
        consumerGroup = "${rocketmq.topic.orderly}",
        selectorExpression = "*",
        enableMsgTrace = true,
        customizedTraceTopic = "${rocketmq.consumer.customized-trace-topic}",
        consumeMode = ConsumeMode.ORDERLY)
@Component
@Slf4j
public class MqConsumerOrderly extends BaseRocketMQListener<Order> {

    @Override
    protected void doConsumerMsg(String message) {
        log.info("接收顺序消息order -> {}", message);

        Order msg = null;
        try {
            msg = objectMapper.readValue(message, Order.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        if (msg.getAmount() == 22) {
            throw new RuntimeException("error");
        }
    }
}